﻿using CommonUtil;
using CommonUtil.Model;
using DataAccess;
using log4net;
using MySql.Data.MySqlClient;
using System;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace view
{
    public partial class PersonFrm : Form
    {
        /// <summary>
        /// 日志对象
        /// </summary>
        private ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// App配置信息
        /// </summary>
        private AppConfigInfo appInfo;

        /// <summary>
        /// 构造方法
        /// </summary>
        public PersonFrm()
        {
            log.Debug("构造方法初期化开始");
            InitializeComponent();

            // 获得配置信息
            appInfo = ConfigManager.getConfigInfo();
            log.Debug("构造方法初期化结束");
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            var selectSql = "SELECT ID, GroupId, Personid, Name, Birthday , SexMale, Mobile, Email, DeptName, Position , Nationality, Hobby FROM Person ";
            var selectWhereSql = "";
            if (txtSearchName.Text != "")
            {
                selectWhereSql += " Name LIKE '%" + txtSearchName.Text + "%'";
            }
            if (txtSearchMobile.Text != "")
            {
                selectWhereSql += (selectWhereSql == "" ? "" : " AND ") + " Mobile LIKE '%" + txtSearchMobile.Text + "%'";
            }
            selectSql += selectWhereSql == "" ? "" : " WHERE " + selectWhereSql;
            // SELECT * FROM Person
            var tmpinfo = MySqlCommonHelper.GetDataSet(MySqlCommonHelper.Conn, CommandType.Text, selectSql, null).Tables[0].DefaultView;

            DataTable dt = new DataTable();
            DataRow dr;
            foreach (var item in tmpinfo)
            {
                dr = dt.NewRow();
            }
            dgvDetail.DataSource = tmpinfo;
        }

        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (hidEditModel.Text == "")
                {
                    // 新规模式的时候
                    string sqlcommand = "INSERT INTO Person(GroupId, Personid, Name, Birthday , SexMale, Mobile, Email, DeptName, Position , Nationality, Hobby)VALUES(@GroupId, @Personid, @Name, @Birthday , @SexMale, @Mobile, @Email, @DeptName, @Position , @Nationality, @Hobby)";
                    MySqlParameter GroupId = new MySqlParameter("@GroupId", MySqlDbType.VarChar, 40);
                    GroupId.Value = appInfo.FaceGroupName;
                    MySqlParameter Personid = new MySqlParameter("@Personid", MySqlDbType.VarChar, 40);
                    Personid.Value = Guid.NewGuid().ToString();
                    MySqlParameter Name = new MySqlParameter("@Name", MySqlDbType.VarChar, 20);
                    Name.Value = txtName.Text;
                    MySqlParameter Birthday = new MySqlParameter("@Birthday", MySqlDbType.DateTime);
                    Birthday.Value = dtBirthday.Value;
                    MySqlParameter SexMale = new MySqlParameter("@SexMale", MySqlDbType.VarChar, 1);
                    SexMale.Value = radSexMale.Checked ? "男" : "女";
                    MySqlParameter Mobile = new MySqlParameter("@Mobile", MySqlDbType.VarChar, 15);
                    Mobile.Value = txtMobile.Text;
                    MySqlParameter Email = new MySqlParameter("@Email", MySqlDbType.VarChar, 50);
                    Email.Value = txtEmail.Text;
                    MySqlParameter DeptName = new MySqlParameter("@DeptName", MySqlDbType.VarChar, 20);
                    DeptName.Value = cmbDeptName.Text;
                    MySqlParameter Position = new MySqlParameter("@Position", MySqlDbType.VarChar, 20);
                    Position.Value = txtPosition.Text;
                    MySqlParameter Nationality = new MySqlParameter("@Nationality", MySqlDbType.VarChar, 20);
                    Nationality.Value = cmbNationality.Text;
                    MySqlParameter Hobby = new MySqlParameter("@Hobby", MySqlDbType.VarChar, 255);
                    Hobby.Value = txtHobby.Text;

                    MySqlParameter[] parameters = { GroupId, Personid, Name, Birthday, SexMale, Mobile, Email, DeptName, Position, Nationality, Hobby };

                    var addCnt = MySqlCommonHelper.ExecuteNonQuery(MySqlCommonHelper.Conn, CommandType.Text, sqlcommand, parameters);

                    if (addCnt > 0)
                    {
                        ClearCntrValue(groupBox2);
                        MessageBox.Show("新规成功");
                    }
                    else
                    {
                        MessageBox.Show("新规失败");
                    }
                }
                else
                {
                    // 更新模式
                }
            }
            catch (Exception)
            {

                MessageBox.Show("新规失败");
                throw;
            }
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearCntrValue(groupBox2);
        }

        /// <summary>
        /// 行双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvDetail_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var selectRow = dgvDetail.CurrentRow;

        }

        /// <summary>
        /// 表格显示行番号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvDetail_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                                                e.RowBounds.Location.Y,
                                                dgv.RowHeadersWidth - 4,
                                                e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                                    dgv.RowHeadersDefaultCellStyle.Font,
                                    rectangle,
                                    dgv.RowHeadersDefaultCellStyle.ForeColor,
                                    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }





        /// <summary>
        /// 清除容器里面某些控件的值
        /// </summary>
        /// <param name="parContainer">容器类控件</param>
        private void ClearCntrValue(Control parContainer)
        {
            for (int index = 0; index < parContainer.Controls.Count; index++)
            {
                // 如果是容器类控件，递归调用自己
                if (parContainer.Controls[index].HasChildren)
                {
                    ClearCntrValue(parContainer.Controls[index]);
                }
                else
                {
                    switch (parContainer.Controls[index].GetType().Name)
                    {
                        case "TextBox":
                            parContainer.Controls[index].Text = "";
                            break;
                        case "RadioButton":
                            ((RadioButton)(parContainer.Controls[index])).Checked = false;
                            break;
                        case "CheckBox":
                            ((CheckBox)(parContainer.Controls[index])).Checked = false;
                            break;
                        case "ComboBox":
                            ((ComboBox)(parContainer.Controls[index])).Text = "";
                            break;
                    }
                }
            }
        }

    }
}
